MenuController = $.Controller.extend('menu', {
	".new_file click" : function(params){
		path = params.element.getAttribute('folder');
		this.element = MVC.$E(path);
		if(path == '/') path = '';
		new MVC.Ajax('/ftp/new_file?path='+path, 
		{onSuccess: this.continue_to('update_directory'), asynchronous:true, evalScripts:false});
		this.title = this.element.innerHTML;
		this.element.style.color = 'Gray';
		this.element.innerHTML = 'creating new file ... ';
	},
	update_directory : function(request){
		eval('var object = '+request.responseText);
		this.element.style.color = '';
		this.element.innerHTML = this.title;
		if(object.response != 'New File' && object.response != 'Dir' && object.response != 'New Folder' && object.response != 'Upload' && object.response != 'Renamed File' && object.response != 'Renamed Folder'){
			alert(object.response); return;
		}
		DirectoryView.update(object);
		new Effect.Highlight( [object.directory,object.new_file].join('/'), {startcolor: '#C3D9FF', duration: 0.7});
	},
	".new_folder click" : function(params){
		path = params.element.getAttribute('folder');
		this.element = MVC.$E(path);
		if(path == '/') path = '';
    	new MVC.Ajax('/ftp/new_folder?path='+path, 
		{onSuccess: this.continue_to('update_directory'), asynchronous:true, evalScripts:false});
		this.title = this.element.innerHTML;
		this.element.style.color = 'Gray';
		this.element.innerHTML = 'creating new folder ... ';
	},
	".rename click" : function(params){
		this.element = MVC.$E(params.element.getAttribute('folder'));
		this.name = new MVC.JFile(this.element.id).file_and_extension();
		this.render({to: this.element});
		//this.attach_to(this.element.firstDescendant(), 'rename');
		JITS.rightClickMenu.clear();
		params.event.kill();
		MVC.$E('RENAME_FORM_NEW_NAME').focus();
	},
	".delete click" : function(params){
		var element = MVC.$E(params.element.getAttribute('folder'));
		var isFile = element.className.startsWith('file');
		if(include.get_env() != 'test' && ! (isFile ? confirm("Delete File?") : confirm("Delete Folder?")) ) return false;
		
		var path = element.id;
		this.path = path;
		element.style.color ='Gray';
		element.innerHTML = 'deleting ...';
		
		if( isFile )
	    	new MVC.Ajax('/ftp/delete_file?path='+path, 
			{onSuccess: this.continue_to('file_deleted'), asynchronous:true, evalScripts:false});
		else
			new MVC.Ajax('/ftp/delete_folder?path='+path, 
			{onSuccess: this.continue_to('file_deleted'), asynchronous:true, evalScripts:false});
		
		
	},
	file_deleted : function(response){
		eval('var object = '+response.responseText);
		if(object.response != 'Delete File'){ 
            alert(object.response);
            MVC.$E(this.path).innerHTML =  new MVC.JFile(this.path).file_and_extension();
            return; 
        }

		editAreaLoader.closeFile('EDITAREA_TEXTAREA', object.path );
		var el = MVC.$E(object.path);
		if(el.className.startsWith('folder') ){
			el.next().remove();
		}
		Effect.DropOut(el, { afterFinish: function(arg){
			 	var element = arg.effects[0].element;
				var parent = element.parentNode;
				arg.effects[0].element.remove();
				if(parent.childNodes.length == 0) parent.style.display = 'none';  //for ie 6
		 } } );
	},
	".upload click" : function(params){
		var path = params.element.getAttribute('folder');
		MVC.$E("Upload").style.display="block";
		var name = new MVC.JFile(path).file_and_extension();
		/*if(frames["multipleupload"]) {
			var frame = frames["multipleupload"];
			frame.
			frame.start(path);
		} else {
			var frame = MVC.$E('multipleupload');
			frame.contentDocument.getElementById('upload_form_folder_name').innerHTML = new MVC.JFile(path).file_and_extension();
			frame.contentWindow.start(path);
		}*/
        MVC.$E('upload_form_folder_name').innerHTML = new MVC.JFile(path).file_and_extension() || '/' ;
        MainController.fancy_upload.options.url = '/upload?_ftp_session_id='+ readCookie('_ftp_session_id')+'&path='+path
        MainController.set_upload_path(path);
        //start(path);
		set_height();
		var ps = new MVC.Controller.Params({event: params.event, element: MVC.$E(path), leave_state: true });
		MVC.Controller.dispatch('directories', 'dblclick', ps);
	}
});


